/**
 * View
 * The View class is part of the MVC design pattern.
 * Use this class to display any controls and data.
 * This is an observer object and will listen for changes from the model.
 * This is an abstract class and is meant to be extended.
 *
 * @author		Craig Phares
 * @version		3.3
 */
package com.rad.mvc {

	import flash.display.Sprite;
	import flash.events.Event;

	public class View extends Sprite implements IView {
		
		private var _model:IModel;           // the model - used for data manipulation
		private var _controller:IController; // the controller - used to handle user initiated events
	
		/**
		 * Creates an instance of the View class.
		 * Passes and saves the model and controller for the MVC unit.
		 * Also passes and saves the target sprite to hold the view in Flash.
		 * If the controller is undefined, the default controller will be used instead.
		 * 
		 * @param m		the model used to render view
		 * @param c		the controller to handle all events
		 *				usually set within defaultController method
		 */
		public function View(m:IModel, c:IController = null) {
			setModel(m);
			setController(c);
		}
			
		/**
		 * Saves the model.
		 *
		 * @param m		the model
		 */
		public function setModel(m:IModel):void {
			_model = m;
			_model.addEventListener(Event.CHANGE, update);
		} 
	
		/**
		 * Retrieves the model.
		 *
		 * @return		the model
		 */
		public function getModel():IModel { return _model; } 
	
		/**
		 * Saves the controller.
		 *
		 * @param c		the controller
		 */
		public function setController(c:IController):void {
			_controller = c;
			if (_controller != null) getController().setView(this);
		} 
	
		/**
		 * Retrieves the controller.
		 *
		 * @return		the controller
		 */
		public function getController():IController {
			if (_controller == null) {
				setController(defaultController(getModel()));
			}
			return _controller;
		}
		
		/**
		 * Retrieves the default controller class for the view.
		 *
		 * @param m		the model
		 * @return		the default controller
		 */
		public function defaultController(m:IModel):IController { return null; } 
	
		/**
		 * Handles any changes received from the observed model.
		 *
		 * @param m		the model that triggered the update
		 * @param n		the notice containing data from the model
		 */
		public function update(e:Event):void {}
									
	}

}